<?php 
// var_dump(file_get_contents('https://www.baidu.com'));
if (@$_POST['Signature'] == 'get') {
	die(json_encode(array('signature'=>create_sign())));
}elseif(@$_POST['fileId'] != ''){
	$_POST['retMsg'] = '转码处理成功';
	die(json_encode(video_to_m3u8()));
}

function video_to_m3u8(){
	$secret_id = "";
	$secret_key = "";

	$host = 'https://vod.api.qcloud.com/v2/index.php';
	$option = array(
		'Action'		=>	'ConvertVodFile',
		'fileId'		=>	$_POST['fileId'],
		'isWatermark'	=>	1,
		'Region'		=>	'cd',
		'Timestamp'		=>	time(),
		// 'Signature'		=>	create_sign(),
		'SecretId'		=>	$secret_id,
		);
	$option['Nonce'] = $option['Timestamp']+rand(100,999999);
	ksort($option);
	$url = get_Api_Signs($host,$option,$secret_key);
	$ret['url'] = $url;
	return $ret;
}

function get_Api_Signs($host,$option,$secret_key,$reqType='GET'){
	preg_match('/https\:\/\//',$host,$hostArr);
	if ($hostArr) {
		$host = str_replace('https://',$reqType,$host);
		$host_header = 'https://';
	}
	preg_match('/http\:\/\//',$host,$hostArr);
	if ($hostArr) {
		$host = str_replace('https://',$reqType,$host);
		$host_header = 'http://';
	}
	foreach ($option as $k => $v) {
		@$_get .= $_get?'&'.$k.'='.$v:'?'.$k.'='.$v;
	}
	$srcStr = $host.$_get;
	$signStr = base64_encode(hash_hmac('sha1', $srcStr, $secret_key, true));
	$srcStr .= '&Signature='.$signStr;
	return str_replace($reqType,$host_header,$srcStr);
}

function get_api_sign($secret_id,$secret_key,$option){
	$srcStr = 'GETvod.api.qcloud.com/v2/index.php?Action='.$option['Action'].'&Nonce='.$option['Nonce'].'&Region='.$option['Region'].'&SecretId='.$option['SecretId'].'&Timestamp='.$option['Timestamp'].'&fileId='.$option['fileId'].'&isWatermark='.$option['isWatermark'];
	$signStr = base64_encode(hash_hmac('sha1', $srcStr, $secret_key, true));
	return $signStr;
}

function get_url($url)   
{  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL,$url);  //设置访问的url地址     
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);      
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容  
    $result =  curl_exec($ch);  
    curl_close ($ch);  
    return $result;   
}  

// 生成上传视频签名
function create_sign(){
	$secret_id = "";
	$secret_key = "";

	// 确定签名的当前时间和失效时间
	$current = time();
	$expired = $current + 86400;  // 签名有效期：1天

	// 向参数列表填入参数
	$arg_list = array(
	    "secretId" => $secret_id,
	    "currentTimeStamp" => $current,
	    "expireTime" => $expired,
	    "random" => rand());

	// 计算签名
	$orignal = http_build_query($arg_list);
	$signature = base64_encode(hash_hmac('SHA1', $orignal, $secret_key, true).$orignal);
	return $signature;
}
?>

<html>
	<head>
		<title>腾讯云视频上传</title>
	</head>
	<style>
	</style>
	<body>
		<form action="">
			<input type="file" id="select_file">
		</form>
	</body>
	<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
	<script src="//imgcache.qq.com/open/qcloud/js/vod/sdk/ugcUploader.js"></script>
	<script type="text/javascript">

		// 1.获取上传签名
		var getSignature = function(callback){
		    $.ajax({
		        url: '',  //获取客户端上传签名的 URL
		        type: 'POST',
		        data: {Signature:'get'},
		        dataType: 'json',
		        success: function(result){//result 是派发签名服务器的回包
		            //假设回包为 { "code": 0, "signature": "xxxx"  }
		            //将签名传入 callback，SDK 则能获取这个上传签名，用于后续的上传视频步骤。
		            callback(result.signature);
		        }
		    });
		};
	 
	$('#select_file').on('change', function (e) {   

		console.log('选择了文件'+this.files[0].name);
		console.log('开始上传文件'+this.files[0].name);
		videoFile = this.files[0];
		qcVideo.ugcUploader.start({
		    videoFile: videoFile,
		    getSignature: getSignature,
		    error: function(result){
		        console.log('上传失败的文件类型：' + result.type);
		        console.log('上传失败的原因：' + result.msg);
		    },
		    progress: function(result){
		        console.log('上传进度：' + Math.floor(result.shacurr*100)+'%');
		    },
		    finish: function(result){
		        console.log('上传结果的 fileId：' + result.fileId);
		        console.log('上传结果的视频名称：' + result.videoName);
		        console.log('上传结果的视频地址：' + result.videoUrl);
		        console.log('上传结果的封面名称：' + result.coverName);
		        console.log('上传结果的封面地址：' + result.coverUrl);
		        $.ajax({
			        url: '',  //获取客户端上传签名的 URL
			        type: 'POST',
			        data: result,
			        dataType: 'json',
			        success: function(result){//result 是派发签名服务器的回包
			        	console.log(result);
			        }
			    });
		    }
		});   
	
	}); 


	</script>
</html>